﻿using PoliticalClassifier.Support;
using System;
using System.Collections;
using System.Collections.Generic;

namespace PoliticalClassifier.Framework
{
    /// <summary>
    /// WeightManager class responsible to manage weights in a single application instance.
    /// </summary>
    public class WeightManager : IWeightManager
    {
        #region Construction
        /// <summary>
        /// Construct a weight manager object.
        /// </summary>
        public WeightManager()
        {

        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Add weight to a WeightManager.
        /// </summary>
        /// <param name="weight">IWeightAttribute instance</param>
        /// <returns>returns the index of added weight</returns>
        public int AddWeight(IWeightAttribute weight)
        {
            for (int i = 0; i < WeightList.Count; i++)
            {
                if (WeightList[i].Name.Equals(weight.Name, StringComparison.OrdinalIgnoreCase))
                {
                    WeightList.RemoveAt(i);
                    break;
                }
            }

            WeightList.Add(weight);
            
            return Count - 1;
        }

        /// <summary>
        /// Remove weight at a given index.
        /// </summary>
        /// <param name="index">Index of weight to be removed</param>
        public void RemoveWeight(int index)
        {
            if (index >= Count)
                return;

            WeightList.RemoveAt(index);
        }

        /// <summary>
        /// Update a value of weight from weightAttribute in a WeightManager
        /// </summary>
        /// <param name="weightAttribute">weightAttribute representing a weight name and new values.</param>
        public void UpdateWeight(IWeightAttribute weightAttribute)
        {
            for (int i = 0; i < WeightList.Count; i++)
            {
                if (WeightList[i].Name.Equals(weightAttribute.Name, StringComparison.OrdinalIgnoreCase))
                {
                    WeightList[i].Value = weightAttribute.Value;
                    break;
                }
            }
        }

        /// <summary>
        /// Get weight values using name.
        /// </summary>
        /// <param name="name">name of the weightattribute.</param>
        /// <returns></returns>
        public Point2D this[String name]
        {
            get
            {
                for (int i = 0; i < WeightList.Count; i++)
                {
                    if (WeightList[i].Name.Equals(name, StringComparison.OrdinalIgnoreCase))
                        return WeightList[i].Value;
                }

                return new Point2D(1, 1);
            }
        }

        /// <summary>
        /// Returns the IWeightAttribute at the given index.
        /// </summary>
        /// <param name="index">The index of the IWeightAttribute.</param>
        /// <returns>The IWeightAttribute at the index.</returns>
        public IWeightAttribute this[int index]
        {
            get { return WeightList[index]; }
        }

        /// <summary>
        /// Gets an enumerator over each of the weight attributes.
        /// </summary>
        /// <returns>An enumerator.</returns>
        public IEnumerator<IWeightAttribute> GetEnumerator()
        {
            return WeightList.GetEnumerator();
        }

        /// <summary>
        /// Gets an enumerator over each of the weight attributes.
        /// </summary>
        /// <returns>An enumerator.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)WeightList).GetEnumerator();
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Get count of Weight Manager.
        /// </summary>
        public int Count
        {
            get { return m_listWeight.Count; }
        }
        #endregion

        #region Private Properties
        /// <summary>
        /// The list of weight attributes.
        /// </summary>
        public IList<IWeightAttribute> WeightList
        {
            get { return m_listWeight; }
        }
        #endregion

        #region Private Fields
        IList<IWeightAttribute> m_listWeight = new List<IWeightAttribute>();
        #endregion
    }
}
